package com.hnucm.c202001020214;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.c202001020214.fragment.Warehouse;
import com.hnucm.c202001020214.object.Book;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {
    public List<Book> bookList;


    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView bookImageId;
        TextView bookName,bookPrice;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            bookImageId = (ImageView) itemView.findViewById(R.id.bookImage);
            bookName = (TextView) itemView.findViewById(R.id.bookName);
            bookPrice = (TextView) itemView.findViewById(R.id.bookPrice);
        }
    }

    public BookAdapter(List<Book> mBookList){
        bookList = mBookList;
    }

    @NonNull
    @Override
    public BookAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_list,parent,false);
        ViewHolder holder = new ViewHolder(view);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Book bookList1 = bookList.get(position);
                Toast.makeText(view.getContext(), bookList1.bookName+"已加入购物车", Toast.LENGTH_SHORT).show();
                Log.i("asdf",bookList1.bookName+bookList1.bookName);

                EventBus.getDefault().post(bookList1);

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BookAdapter.ViewHolder holder, int position) {
        Book  book = bookList.get(position);
        holder.bookImageId.setImageResource(book.getBookImageId());
        holder.bookName.setText(book.getBookName());
        holder.bookPrice.setText(book.getBookPrice());

    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }
}
